#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <unordered_map>
#include <vector>
#include <set>
#include <queue>

using namespace std;
using LL = long long;
using PII = pair<int, int>;

const int N = 1e6 + 10;

int n;
int q[N];
int cnt;

void down(int u){
    int t = u;
    if(u * 2 <= cnt && q[t] > q[u * 2]) t = u * 2;
    if(u * 2 + 1 <= cnt && q[t] > q[u * 2 + 1]) t = u * 2 + 1;
    if(u != t){
        swap(q[u], q[t]);
        down(t);
    }
}


void up(int u){
    int t = u;
    while(t / 2 && q[t] < q[t / 2]){
        swap(q[t], q[t / 2]);
        t /= 2;
    }
}


int main(){
#ifdef LOCALFLAG
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif


    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> n;
    int op, x;
    while(n --){
        cin >> op;
        if(op == 1){
            cin >> x;
            q[++cnt] = x;
            up(cnt);
        }else if(op == 2){
            cout << q[1] << '\n';
        }else{ // op == 3
            swap(q[1], q[cnt]);
            cnt --;
            down(1);
        }
    }

    return 0;
}